package com.updesignsz.admin.controller;

import com.updesignsz.admin.service.ModuleService;
import com.updesignsz.admin.util.ResponseUtil;
import com.updesignsz.admin.vo.form.ModuleForm;
import com.updesignsz.commom.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * TODO
 * Created User: lx
 * Date: 2020/4/29 15:36
 */
@RestController
public class ModuleController {

    @Autowired
    private ModuleService moduleService;

    @GetMapping("/module")
    public void module(HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        vo.setData(moduleService.listAll());
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }

    @PostMapping("/module")
    public void addModule(@Validated(PostMapping.class) @RequestBody ModuleForm form, HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        moduleService.saveModule(form);
        vo.setData(form);
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }


    @DeleteMapping("/module/{moduleId}")
    public void deleteModule(@PathVariable Integer moduleId, HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        moduleService.deleteModule(moduleId);
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }

    @PutMapping("/module")
    public void updateModule(@Validated(PutMapping.class) @RequestBody ModuleForm form, HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        moduleService.updateModule(form);
        vo.setData(moduleService.getById(form.getId()));
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }
}
